using System;
using System.Linq;
using System.Text;
using System.Web;
using MarsExplorerWebSite.Model;

namespace MarsExplorerWebSite.Functions
{
    public class getAllResourcesFromTeam : FunctionBase
    {
        public int TeamID { get; set; }
        private MarsExplorerEntities dbContext;

        public getAllResourcesFromTeam(HttpContext context)
            : base(context)
        {
            InitializeProperties();
        }

        public override string Execute()
        {
            StringBuilder sb = new StringBuilder();
            try
            {
                var tr = from x in dbContext.TeamsResources where x.TeamID == TeamID select x;
                
                Team g = (from f in dbContext.Teams where f.TeamID==TeamID select f).FirstOrDefault();

                int i = 0;
                foreach (TeamsResource t in tr)
                {
                    i += 1;
                    TeamsTurn trt = (from c in dbContext.TeamsTurns where c.TeamID == TeamID && c.ResourceID==t.ResourceID select c).FirstOrDefault();
                    int v = t.Value - trt.Value;
                    sb.Append( v+";");
                   
                }
                sb.Append(g.Credits);
                return sb.ToString();
            }
            catch (Exception)
            {

                return "ERROR";
            }
          
           
        }

        public override void InitializeProperties()
        {
            try
            {
                TeamID = Convert.ToInt32(Parameters["teamid"]);
                dbContext = new MarsExplorerEntities();
            }
            catch (Exception e)
            {
                string message = "";
                #if DEBUG
                message += " : " + e.ToString();
                #endif
            }
        }
    }

   
}